﻿using OneP.InfinityScrollView;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RoomPanel : InfinityBaseItem
{
    public Text RoomName;
    public Text Difen;
    public Text Number;
    public Text AllNumber;
    public Text time;
    public Text Name;
    public Image Jx;
    public Image CY;
    public Image ready;
    public RawImage Head;
    public string RoomID;
    
    public int TypeID;//0代表进行中，1代表俱乐部房间
    public override void Reload(InfinityScrollView _infinity, int _index)
    {
        if (TypeID==0)
        {
            RoomID= SaveDate.GetInstance().GetRoomRet.roomMsg[_index].RoomId;
            RoomName.text = SaveDate.GetInstance().GetRoomRet.roomMsg[_index].RoomName;
            time.text = SaveDate.GetInstance().GetRoomRet.roomMsg[_index].Time;
            Name.text = SaveDate.GetInstance().GetRoomRet.roomMsg[_index].Name;
            Difen.text = SaveDate.GetInstance().GetRoomRet.roomMsg[_index].Difen.ToString();
            Number.text = SaveDate.GetInstance().GetRoomRet.roomMsg[_index].NowNumber.ToString();
            AllNumber.text = SaveDate.GetInstance().GetRoomRet.roomMsg[_index].AllNumber.ToString();
            if (SaveDate.GetInstance().GetRoomRet.roomMsg[_index].GetType==0)
            {
                CY.gameObject.SetActive(false);
            }
            else if (SaveDate.GetInstance().GetRoomRet.roomMsg[_index].GetType == 2)
            {
                CY.gameObject.SetActive(false);
                Jx.gameObject.SetActive(false);
            }
            Debug.Log("后端bug" + SaveDate.GetInstance().GetRoomRet.roomMsg[_index].hade);
            DownloadHeadTexture(Head, SaveDate.GetInstance().GetRoomRet.roomMsg[_index].hade);
        }
        else
        {
                RoomID = SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].RoomId;
                RoomName.text = SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].RoomName;
                time.text = SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].Time;
                Name.text = SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].Name;
                Difen.text = SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].Difen.ToString();
                Number.text = SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].NowNumber.ToString();
                AllNumber.text = SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].AllNumber.ToString();
            if (SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].GetType == 0)
            {
                CY.gameObject.SetActive(false);
            }
            else if(SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].GetType == 2){
                CY.gameObject.SetActive(false);
                Jx.gameObject.SetActive(false);
            }

            Debug.Log("后端bug"+SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].hade);
            DownloadHeadTexture(Head, SaveDate.GetInstance().GetRoomRet.clubRoomMsg[_index].hade);
        }
       
        }
    public void JoinRoom() {
        Debug.Log(RoomID+"加入房间");
    }

    protected void DownloadHeadTexture(RawImage texture, string url)            //下载头像图片
    {
        //texture = null;
#if WX_SDK
           if (url != null)
           {
            //用户头像，最后一个数值代表正方形头像大小（有0、46、64、96、132数值可选，0代表640*640正方形头像），用户没有头像时该项为空
            //url = url.Substring(0, url.Length - 1);
            //url += "132";    
            StartCoroutine(DownloadTexture(texture, url));     
           }
           else
           {
              Debug.Log("下载头像Texture收到的url为空！");     
           }
#else
        url = URLConst.DefaultUserHeadUrl;
        StartCoroutine(DownloadTexture(texture, url));
#endif
    }

    private IEnumerator DownloadTexture(RawImage texture, string url)
    {
        WWW www = new WWW(url);
        yield return www;
        texture.texture = www.texture;
    }

    public void Send76()
    {
        Destroy(GameObject.Find("FaXian(Clone)"));
        UIManager.ChangeUI(UIManager.PageState.GameHallPage, (GameObject obj) =>
        {
            obj.GetComponent<GameHallPanel>().InformationSetting();
        });
        MessageManager.GetInstance().SendMsg76(RoomID);
    }
}
